﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GLImp;
using LibNoise;
using System.Drawing;
using LibNoise.Generator;
using LibNoise.Operator;
using LibNoise.Utilities;

namespace UnitTest {
	public class Program{
		private static Noise2D m_noiseMap = null;
		private static Texture[] m_textures = new Texture[3];
		private static Texture[] m_textures2 = new Texture[3];
		private static Camera2D Camera;

		private static Perlin myModule = new Perlin();
		private static NoiseMap heightMap = new NoiseMap();
		private static NoiseMapBuilderPlane heightMapBuilder = new NoiseMapBuilderPlane();

		public static void Main() {
			GraphicsManager.SetTitle("LibNoise Unit Test");
			GraphicsManager.SetResolution(600, 400);
			GraphicsManager.SetBackground(Color.Black);
			GraphicsManager.EnableMipmap = false;
			Camera = new Camera2D();
			Camera.OnRender += GraphicsManager_Render;
			GraphicsManager.Update += new GraphicsManager.Updater(GraphicsManager_Update);
			Initialize();
			GraphicsManager.Start();
		}

		private static void Initialize() {
			// Create the module network
			Perlin perlin = new Perlin();
			RiggedMultifractal rigged = new RiggedMultifractal();
			Add add = new Add(perlin, rigged);

			// Initialize the noise map
			m_noiseMap = new Noise2D(64, 64, add);
			m_noiseMap.GeneratePlanar(-1, 1, -1, 1);

			// Generate the textures
			m_textures[0] = new Texture(m_noiseMap.GetTexture(Gradient.Grayscale), "", 0, 0);
			m_textures[1] = new Texture(m_noiseMap.GetTexture(Gradient.Terrain), "", 0, 0);
			m_textures[2] = new Texture(m_noiseMap.GetNormalMap(3.0f), "", 0, 0);

			// Zoom in or out do something like this.
			float zoom = 0.5f;
			m_noiseMap.GeneratePlanar(-1 * zoom, 1 * zoom, -1 * zoom, 1 * zoom);

			for (int i = 0; i < 3; i++) {
				heightMapBuilder.SetSourceModule(myModule);
				heightMapBuilder.SetDestNoiseMap(heightMap);
				heightMapBuilder.SetDestSize(16, 16);
				heightMapBuilder.SetBounds(i, i + 1, 0.0, 1.0);
				heightMapBuilder.Build();

				RendererImage renderer = new RendererImage();
				renderer.SetSourceNoiseMap(heightMap);
				Bitmap image = renderer.Render();
				m_textures2[i] = new Texture(image, "");
			}
		}

		static void GraphicsManager_Render() {
			int w = GraphicsManager.WindowWidth / 3;
			m_textures[0].Draw(0, 0, w, w);
			m_textures[1].Draw(w, 0, w, w);
			m_textures[2].Draw(w * 2, 0, w, w);
			for (int i = 0; i < 3; i++) {
				m_textures2[i].Draw(i * w, w, w, w);
			}
		}

		static void GraphicsManager_Update() {
			if (KeyboardManager.IsPressed(OpenTK.Input.Key.Escape)) { 
				Environment.Exit(0); 
			}
		}
	}
}
